package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.entity.ErrorMsg;
import com.itms.enums.TableName;
import com.itms.mapper.PackageMapper;
import com.itms.mapper.TableDataSyncMapper;
import com.itms.pojo.Customer;
import com.itms.pojo.Package;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
@PropertySource("classpath:quartz.properties")
public class PackageJob {

    @Reference
    private BasicDataService basicDataService;
    @Autowired
    private TableDataSyncMapper tableDataSyncMapper;
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private SycService sycService;

    @Scheduled(initialDelayString = "${PackageJob.initialDelayString}",fixedDelayString="${PackageJob.fixedDelayString}" )
    public void deal(){
        try {
            List<ErrorMsg> errorList = new ArrayList<>();

            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBPackage.toString());

            Date itms = this.sycService.getMaxSyncDate(TableName.MDBPackage.toString());

            if(mdb==null){
                return ;
            }

            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) {
                return;
            }

            List<Package> list = basicDataService.getPackageList(itms);

            for (Package p : list) {
                com.itms.entity.Package outpa = null;

                boolean flag = false;

                com.itms.entity.Package pa = packageMapper.getPacakgeByPacakgeCode(p.getPackageCode(),null);      //内包装

                if (p.getPalletPackageCode() != null   ) {
                    flag = true;
                    outpa = packageMapper.getPacakgeByPacakgeCode(p.getPalletPackageCode(),null);     //外包装
                }

                //新增外包装
                if (flag) {
                    if (outpa == null) {
                        //新增
                        outpa = new com.itms.entity.Package();
                        outpa.setPackageCode(p.getPalletPackageCode());
                        outpa.setPackageName(p.getPalletPackageName());
                        outpa.setPackageType("03");

                        if(p.getLength()!=null){
                            outpa.setLength(new BigDecimal(p.getPalletLength().longValue()/10));
                        }
                        if(p.getPalletWidth()!=null){
                            outpa.setWidth(new BigDecimal(p.getPalletWidth().longValue()/10));
                        }
                        if(p.getPalletHeight()!=null){
                            outpa.setHeight(new BigDecimal(p.getPalletHeight().longValue()/10));
                        }
                        if(p.getPalletWeight()!=null){
                            outpa.setWeight(new BigDecimal(p.getPalletWeight().longValue()));
                        }
                        if(p.getPalletLayersQty()!=null){
                            outpa.setLevelNum(new BigDecimal(p.getPalletLayersQty().longValue()));
                        }
                        if(p.getMaxBinsOnPalle()!=null){
                            outpa.setMaxNumberPerTray(new BigDecimal(p.getMaxBinsOnPalle().longValue()));
                        }
                        if(p.getFoldRate()!=null){
                            outpa.setFoldingratio(new BigDecimal(p.getFoldRate().longValue()));
                        }

                        outpa.setCreateDate(new Date());
                        outpa.setIsValid(p.getIsValid());
                        outpa.setLastUpdateDate(new Date());
                        outpa.setLastUpdateUserId(new BigDecimal(9999));
                        packageMapper.insertSelective(outpa);
                    } else {          //修改

                        outpa.setPackageCode(p.getPalletPackageCode());
                        outpa.setPackageName(p.getPalletPackageName());

                        outpa.setPackageType("03");
                        if(p.getLength()!=null){
                            outpa.setLength(new BigDecimal(p.getPalletLength().longValue()/10));
                        }
                        if(p.getPalletWidth()!=null){
                            outpa.setWidth(new BigDecimal(p.getPalletWidth().longValue()/10));
                        }
                        if(p.getPalletHeight()!=null){
                            outpa.setHeight(new BigDecimal(p.getPalletHeight().longValue()/10));
                        }
                        if(p.getPalletWeight()!=null){
                            outpa.setWeight(new BigDecimal(p.getPalletWeight().longValue()));
                        }
                        if(p.getPalletLayersQty()!=null){
                            outpa.setLevelNum(new BigDecimal(p.getPalletLayersQty().longValue()));
                        }
                        if(p.getMaxBinsOnPalle()!=null){
                            outpa.setMaxNumberPerTray(new BigDecimal(p.getMaxBinsOnPalle().longValue()));
                        }
                        if(p.getFoldRate()!=null){
                            outpa.setFoldingratio(new BigDecimal(p.getFoldRate().longValue()));
                        }
                        outpa.setLastUpdateDate(new Date());
                        outpa.setIsValid(p.getIsValid());
                        outpa.setLastUpdateUserId(new BigDecimal(9999));
                        packageMapper.updateByPrimaryKeySelective(outpa);
                    }

                }

                //新增内包装
                if (pa == null) {
                    pa = new com.itms.entity.Package();
                    if (flag == true) {
                        pa.setTrayNo(outpa.getId());

                    }
                    pa.setPackageCode(p.getPackageCode());
                    pa.setPackageName(p.getPackageName());
                    if (p.getPackageType().equals("PACKAGE_TYPE003")) {
                        pa.setPackageType("03");
                    } else if (p.getPackageType().equals("PACKAGE_TYPE001")) {
                        pa.setPackageType("03");
                    } else if (p.getPackageType().equals("PACKAGE_TYPE002")) {
                        pa.setPackageType("03");
                    } else if (p.getPackageType().equals("PACKAGE_TYPE004")) {
                        pa.setPackageType("01");
                    } else if (p.getPackageType().equals("PACKAGE_TYPE005")) {
                        pa.setPackageType("01");
                    } else if (p.getPackageType().equals("PACKAGE_TYPE006")) {
                        pa.setPackageType("01");
                    }

                    if(p.getLength()!=null){
                        pa.setLength(new BigDecimal(p.getLength().longValue()/10));
                    }
                    if(p.getWidth()!=null){
                        pa.setWidth(new BigDecimal(p.getWidth().longValue()/10));
                    }
                    if(p.getHeight()!=null){
                        pa.setHeight(new BigDecimal(p.getHeight().longValue()/10));
                    }
                    if(p.getWeight()!=null){
                        pa.setWeight(new BigDecimal(p.getWeight().longValue()));
                    }
                    if(p.getLayersQty()!=null){
                        pa.setLevelNum(new BigDecimal(p.getLayersQty().longValue()));
                    }
                    if(p.getPackageQty()!=null){
                        pa.setMaxNumberPerTray(new BigDecimal(p.getPackageQty().longValue()));
                    }
                    if(p.getFoldRate()!=null){
                        pa.setFoldingratio(new BigDecimal(p.getFoldRate().longValue()));
                    }

                    pa.setCreateDate(new Date());
                    pa.setLastUpdateDate(new Date());
                    pa.setIsValid(p.getIsValid());
                    pa.setCreateUserId(new BigDecimal(9999));
                    packageMapper.insertSelective(pa);

                } else {
                    if (flag == true) {
                        pa.setTrayNo(outpa.getId());

                        pa.setPackageCode(p.getPackageCode());
                        pa.setPackageName(p.getPackageName());
                        if (p.getPackageType().equals("PACKAGE_TYPE003")) {
                            pa.setPackageType("02");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE001")) {
                            pa.setPackageType("02");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE002")) {
                            pa.setPackageType("02");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE004")) {
                            pa.setPackageType("01");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE005")) {
                            pa.setPackageType("01");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE006")) {
                            pa.setPackageType("01");
                        }

                        if(p.getLength()!=null){
                            pa.setLength(new BigDecimal(p.getLength().longValue()/10));
                        }
                        if(p.getWidth()!=null){
                            pa.setWidth(new BigDecimal(p.getWidth().longValue()/10));
                        }
                        if(p.getHeight()!=null){
                            pa.setHeight(new BigDecimal(p.getHeight().longValue()/10));
                        }
                        if(p.getWeight()!=null){
                            pa.setWeight(new BigDecimal(p.getWeight().longValue()));
                        }
                        if(p.getLayersQty()!=null){
                            pa.setLevelNum(new BigDecimal(p.getLayersQty().longValue()));
                        }
                        if(p.getPackageQty()!=null){
                            pa.setMaxNumberPerTray(new BigDecimal(p.getPackageQty().longValue()));
                        }
                        if(p.getFoldRate()!=null){
                            pa.setFoldingratio(new BigDecimal(p.getFoldRate().longValue()));
                        }
                        pa.setLastUpdateDate(new Date());
                        pa.setIsValid(p.getIsValid());

                        pa.setLastUpdateUserId(new BigDecimal(9999));
                        packageMapper.updateByPrimaryKeySelective(pa);

                    }else{
                        pa.setPackageCode(p.getPackageCode());
                        pa.setPackageName(p.getPackageName());
                        if (p.getPackageType().equals("PACKAGE_TYPE003")) {
                            pa.setPackageType("03");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE001")) {
                            pa.setPackageType("03");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE002")) {
                            pa.setPackageType("03");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE004")) {
                            pa.setPackageType("01");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE005")) {
                            pa.setPackageType("01");
                        } else if (p.getPackageType().equals("PACKAGE_TYPE006")) {
                            pa.setPackageType("01");
                        }

                        if(p.getLength()!=null){
                            pa.setLength(new BigDecimal(p.getLength().longValue()/10));
                        }
                        if(p.getWidth()!=null){
                            pa.setWidth(new BigDecimal(p.getWidth().longValue()/10));
                        }
                        if(p.getHeight()!=null){
                            pa.setHeight(new BigDecimal(p.getHeight().longValue()/10));
                        }
                        if(p.getWeight()!=null){
                            pa.setWeight(new BigDecimal(p.getWeight().longValue()));
                        }
                        if(p.getLayersQty()!=null){
                            pa.setLevelNum(new BigDecimal(p.getLayersQty().longValue()));
                        }
                        if(p.getPackageQty()!=null){
                            pa.setMaxNumberPerTray(new BigDecimal(p.getPackageQty().longValue()));
                        }
                        if(p.getFoldRate()!=null){
                            pa.setFoldingratio(new BigDecimal(p.getFoldRate().longValue()));
                        }
                        pa.setLastUpdateDate(new Date());
                        pa.setIsValid(p.getIsValid());

                        pa.setLastUpdateUserId(new BigDecimal(9999));
                        packageMapper.updateByPrimaryKeySelective(pa);
                    }

                }
            }

            if (errorList.size() == 0) {
                this.sycService.save(mdb, TableName.MDBPackage.toString());
            }
            this.logService.saveErrorList(errorList,TableName.MDBPackage.toString(),this.getClass().getName());

        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e,TableName.MDBPackage.toString(),this.getClass().getName());
        }
    }
}
